// 洗车卡
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    all:[
      {
        value:"我的卡",
        checked: true
      },
      {
        value: "年卡",
        checked: false
      },
      {
        value: "次卡",
        checked: false
      }
    ],
    //我的卡
    card:[],
    //状态
    status: 0,
    paykind: [],
    //分页
    page:1,
    //每页的数量
    listNum: 6,
    //是否正在请求
    req: false,
    //没有更多数据了
    nomore: false,
    //当前选中的价格的优惠套餐
    chose:{},
    options:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.requestList(1);
    console.log(options)
    this.setData({
      options: options
    })
    var that = this;
    //判断是否是从firs页面挑过来的
    if(options.first == "true"){
      var all = that.data.all;
      all.map(function (val) {
        val.checked = false;
      })
      all[1].checked = true;
      new Promise(function () {
        console.log("1")
        that.setData({
          all: all,
          status: 1,
          req: false,
        })
      }).then(that.requestPaycard(1)).catch(function (e) {
        wx.showToast({
          title: e.message,
          icon: "none"
        })
        console.log(e)
      });
    }
    // 是否是选择优惠卡
    if (options.use === "true") {
      that.setData({
        all: [
          {
            value: "我的卡",
            checked: true
          }]
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.getUserInfoFun();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  /**
   * 改变选中的状态
   */
  changeChecked: function (e) {
    var idx = e.currentTarget.dataset.index;
    var all = this.data.all;
    var that = this;
    all.map(function (val) {
      val.checked = false;
    })
    all[idx].checked = true;
    if(idx == 1) {
      new Promise(function(){
        that.setData({
          all: all,
          status: idx,
          req: false,
        })
      }).then(this.requestPaycard(1)).catch(function(e){
        wx.showToast({
          title: e.message,
          icon:"none"
        })
      });
    } else if(idx == 2) {
      new Promise(function () {
        that.setData({
          all: all,
          status: idx,
          req: false,
        })
      }).then(this.requestPaycard(2)).catch(function (e) {
        wx.showToast({
          title: e.message,
          icon: "none"
        })
      });
    } else{
      that.setData({
        all: all,
        status: idx,
        req: false,
      })
      this.requestList(1)
    }
    
  },
  /**
   * 选择购买的金额
   */
  chosePay:function(e){
    var idx = e.currentTarget.dataset.index;
    var paykind = this.data.paykind;
    paykind.map(function(val){
      val.checked = false;
    });
    paykind[idx].checked = true;
    this.setData({
      paykind: paykind,
      chose: paykind[idx]
    })
  },
  /**
   * 下拉触底事件
   */
  scrollBottom: function(){
   var page = this.data.page;
   page = +page + 1;
   this.setData({
     page: page
   })
    this.requestList(page);
  },
  /**
   * 查看卡使用记录
   */
  record:function(e) {
    var card = this.data.card;
    var index = e.currentTarget.dataset.index;
    wx.navigateTo({
      url: '/mine/record/record?y_id=' + card[index].id,
    })
  },
  /**
   * 请求我的年卡
   */
  requestList: function(page){
    var that = this;
    var listNum = this.data.listNum;
    var pagenum = this.data.page;
    var req = this.data.req;
    var u_id = wx.getStorageSync("user").u_id;
    if(req == false) {
      var params = {
        u_id: u_id,
        pagenum: pagenum,
        listNum: listNum
      }
      that.setData({
        req: true
      })
      app.getRequest("api/year/myYearCard",params,function(e){
        if(e.code == 200){
          var list = e.data.list;
          var length = list.length;
          if(length > 0){
            var card = that.data.card;
            if (pagenum === 1) {
              card = [];
            }
            list.map(function(val){
              // val.end_time = app.time(val.end_time);
              card.push(val)
            })
            that.setData({
              card: card,
              req: false
            })
          } else {
            that.setData({
              nomore: true
            })
          }
        } else {
          wx.showToast({
            title: e.message,
            icon:"none"
          })
          that.setData({
            req: false
          })
        }
      })  
    }
  },
  /**
   * 请求充值卡
   */
  requestPaycard: function(t){
    var req = this.data.req;
    var paykind = this.data.paykind;
    var that = this;
    if(req == false) {
      this.setData({
        req: true
      })
      app.getRequest("api/year/yearCardList",{type:t},function(e){
        if(e.code === 200) {
          var cardlist = e.data.list;
          var listLength = cardlist.length;
          if(listLength > 0){
            cardlist.map(function(val){
              val.checked = false
            })
            cardlist[0].checked = true;
            that.setData({
              paykind: cardlist,
              chose: cardlist[0],
              req: false
            })
          } else{
            wx.showToast({
              title: '没有更多了！',
              icon:"none"
            })
          }
        } else {
          wx.showToast({
            title: e.message,
            icon:"none"
          })
        }
      })
    }
  },
  /**
   * 点击洗车卡去使用
   */
  toUse: function(e){
    var options = this.data.options;
    if (options.use === "true") {
      var card = this.data.card;
      var index = e.currentTarget.dataset.index;
      wx.redirectTo({
        url: '/use/sure/sure?card=' + JSON.stringify(card[index]),
      })
    } 
  },
  pay: function(){
    var status = this.data.status;
    var y_id = this.data.chose.id;
    // console.log(this.data.chose);
    var u_id = wx.getStorageSync("user").u_id;
    var params = {
      u_id: u_id,
      y_id: y_id,
      "type": status
    }
    app.getRequest("api/year/wxPay",params,function(e){
      if(e.code === 200) {
        var res = JSON.parse(e.data);
        wx.requestPayment({
          timeStamp: res.timeStamp,
          nonceStr: res.nonceStr,
          package: res.package,
          signType: res.signType,
          paySign: res.paySign,
          success(res) {
            wx.showToast({
              title: "充值成功",
            });
            wx.redirectTo({
              url: '/mine/card/card',
            })
            // app.getRequest("api/login/userlogin", params, function (rres) {
            //   if (rres.code == 200) {
            //     wx.setStorageSync("u_id", rres.data.u_id);
            //     wx.setStorageSync("user", rres.data);
            //     wx.redirectTo({
            //       url: '/mine/card/card',
            //     })
            //   } else {
            //     wx.showToast({
            //       title: rres.message,
            //       icon: "none"
            //     })
            //   }
            // });
          },
          fail(res) {
            wx.showModal({
              title: '',
              content: "支付失败",
              showCancel: false
            })
          }
        })
      }
    })
  }
})